package ma.dev.sowondejong.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import com.actionbarsherlock.app.SherlockFragmentActivity;

import org.jetbrains.annotations.NotNull;

import ma.dev.sowondejong.R;
import ma.dev.sowondejong.R.drawable;
import ma.dev.sowondejong.util.BackPressCloseHandler;
import ma.dev.sowondejong.util.MusicController;
import ma.dev.sowondejong.view.RecieveProtocolDialog;

@SuppressLint("ValidFragment")
public class MainActivity extends SherlockFragmentActivity implements OnClickListener {

	private ImageView ivJong;
	private ImageView btnEmile, btnSowon;
	private MusicController musicController;
	private BackPressCloseHandler backHandler;
	private int mode;
	private final int EMILE = 1;
	private final int SOWON = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);
        ImageView btnInfo, btnSend;

		ivJong = (ImageView)findViewById( R.id.iv_jong );
		ivJong.setOnClickListener(this);

		btnEmile = (ImageView)findViewById( R.id.btn_emile );
		btnEmile.setOnClickListener(this);
		btnEmile.setImageResource(R.drawable.main_bell_tab_em_selected);

		btnSowon = (ImageView)findViewById( R.id.btn_sowon );
		btnSowon.setOnClickListener(this);

		btnInfo = (ImageView)findViewById(R.id.iv_info);
		btnInfo.setOnClickListener(this);

		btnSend = (ImageView)findViewById(R.id.iv_send_icon);
		btnSend.setOnClickListener(this);

		musicController = new MusicController(this, R.raw.emile_sound);
		backHandler = new BackPressCloseHandler(this);

	}

	@Override
	public void onBackPressed() {
		backHandler.onBackPressed();
	}

	@Override
	public void onClick(@NotNull View v) {

		switch(v.getId())
		{
		case R.id.iv_jong :
			musicController.play();	


			AnimationSet animationSet = new AnimationSet(true);


			RotateAnimation r = new RotateAnimation(-10f, 8f, ivJong.getWidth() /2, 0); // HERE 
			r.setRepeatMode(Animation.REVERSE);
			r.setRepeatCount(Animation.INFINITE);
			r.setDuration(3000);
			r.setFillAfter(true); //HERE
			animationSet.addAnimation(r);


			ivJong.startAnimation(animationSet);

			if(mode == SOWON) {
				RecieveProtocolDialog dlg = new RecieveProtocolDialog();
				dlg.show(getSupportFragmentManager(), "recieve_protocol");			
			}

			break;

		case R.id.btn_emile :

			ivJong.setImageResource(R.drawable.main_bell_em);
			btnEmile.setImageResource(R.drawable.main_bell_tab_em_selected);
			btnSowon.setImageResource(R.drawable.main_bell_tab_so);

			mode = EMILE;

			break;

		case R.id.btn_sowon :

			ivJong.setImageResource(drawable.main_bell_so);
			btnEmile.setImageResource(R.drawable.main_bell_tab_em);
			btnSowon.setImageResource(R.drawable.main_bell_tab_so_selected);

			mode = SOWON;

			break;
		case R.id.iv_info :
			startActivity(new Intent(this, InfoActivity.class));
			finish();
			break;

		case R.id.iv_send_icon : 
			startActivity(new Intent(this, SowonListActivity.class));
			finish();

			break;
		}
	}


}
